package question.solution17;

import question.SolutionTemplate;

import java.util.*;

public class Solution17 implements SolutionTemplate {

    public List<String> letterCombinations(String digits) {
        List<String> result = new ArrayList<>();

        if(digits.length() == 0){
            return result;
        }
        Map<Character, List<Character>> numberCharactorMap = getMap();
        StringBuilder stringBuilder = new StringBuilder();
        getSubString(result, digits, stringBuilder, numberCharactorMap);

        return result;

    }

    private void getSubString(List<String> result, String digits, StringBuilder parentString, Map<Character, List<Character>> numberCharactorMap){
        if(digits.length() == 0){
            result.add(parentString.toString());
            return;
        }
        char digit = digits.charAt(0);
        List<Character> characterList = numberCharactorMap.get(digit);
        for(Character character : characterList){
            digits = digits.substring(1);
            parentString.append(character);

            getSubString(result, digits, parentString, numberCharactorMap);

            digits = digit + digits;
            parentString.deleteCharAt(parentString.length()-1);
        }
    }

    private Map<Character, List<Character>> getMap(){
        Map<Character, List<Character>> numberCharactorMap = new HashMap<>();
        List<Character> charactors2 = new ArrayList<>();
        charactors2.add('a');
        charactors2.add('b');
        charactors2.add('c');
        numberCharactorMap.put('2', charactors2);

        List<Character> charactors3 = new ArrayList<>();
        charactors3.add('d');
        charactors3.add('e');
        charactors3.add('f');
        numberCharactorMap.put('3', charactors3);

        List<Character> charactors4 = new ArrayList<>();
        charactors4.add('g');
        charactors4.add('h');
        charactors4.add('i');
        numberCharactorMap.put('4', charactors4);

        List<Character> charactors5 = new ArrayList<>();
        charactors5.add('j');
        charactors5.add('k');
        charactors5.add('l');
        numberCharactorMap.put('5', charactors5);

        List<Character> charactors6 = new ArrayList<>();
        charactors6.add('m');
        charactors6.add('n');
        charactors6.add('o');
        numberCharactorMap.put('6', charactors6);

        List<Character> charactors7 = new ArrayList<>();
        charactors7.add('p');
        charactors7.add('q');
        charactors7.add('r');
        charactors7.add('s');
        numberCharactorMap.put('7', charactors7);

        List<Character> charactors8 = new ArrayList<>();
        charactors8.add('t');
        charactors8.add('u');
        charactors8.add('v');
        numberCharactorMap.put('8', charactors8);

        List<Character> charactors9 = new ArrayList<>();
        charactors9.add('w');
        charactors9.add('x');
        charactors9.add('y');
        charactors9.add('z');
        numberCharactorMap.put('9', charactors9);

        return numberCharactorMap;
    }

    @Override
    public void test() {
        System.out.println(letterCombinations("2"));

    }

}
